﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BL;
using Data;
using BaseUI;
using IUI;

namespace WinUI
{
    public partial class LoginForm : //Form
                                     BaseWinForm<LoginController>, ILogin
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private event EventHandler _click_LoginButton;
        private void btn_Login_Click(object sender, EventArgs e)
        {
            if (_click_LoginButton != null) _click_LoginButton(this, null);
        }

        #region Реализация ILogin

        public String strLogin
        {
            get { 
                return txtBx_Login.Text; 
            }
        }
               
        public String strPasswd
        {
            get {
                return txtBx_Passwd.Text; 
            }
        }

        public EventHandler Click_LoginButton
        {
            get { return _click_LoginButton; }
            set { _click_LoginButton = value; }
        }

        public void ShowError()
        {
            //if (empty)
            //{
            //    MessageBox.Show("Login & Password fields shouldn't be empty",
            //                    "Input Error",
            //                    MessageBoxButtons.OK);
            //}
            //else
            //{
                MessageBox.Show("Check Login and Password",
                                "Login State",
                                MessageBoxButtons.OK);
            //}
        }

        public void Success(User us)
        {
            User.AddToAllUsers(us);
            MainForm.Instance.SetUserID(us.Name);
            MainForm.Instance.Show();
            this.Close();
        }

        #endregion

        private void txtBx_Login_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == '\r')
            {
                this.btn_Login_Click(this, new EventArgs());
            }
        }

        private void txtBx_Passwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btn_Login_Click(this, new EventArgs());
            }
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (null == MainForm.Instance.UserID)
            {
                MainForm.Instance.Close();
            }
        }

    }
}
